package com.persondemo.videoappliction.ui.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.MeCollectBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.ui.adapter.MecollectAdapter;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.duanzi.DuanziDetilsActivity;
import com.persondemo.videoappliction.ui.tv.TvActivity;
import com.persondemo.videoappliction.ui.vip.contract.CollectContract;
import com.persondemo.videoappliction.ui.vip.presenter.CollectPresenter;
import com.persondemo.videoappliction.utils.PreferenceUtil;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;
import com.persondemo.videoappliction.widget.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2018/4/20.
 * 我的收藏
 */

public class CollectActivity extends BaseActivity<CollectPresenter> implements CollectContract.View {
    @BindView(R.id.collect_back)
    ImageView collectBack;
    @BindView(R.id.collect_title)
    TextView collectTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    private MecollectAdapter adapter;
    int page = 1;
    private String imei = "";
    private List<MeCollectBean.DataBean> list;

    @Override
    public void onRetry() {
        initData();
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_collect;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (mPresenter != null) {
                    showLoadingDialog();
                    page = 1;
                    mPresenter.getdata(imei, page + "");
                }
            }
        });
        mRecyclerView.setLayoutManager(new GridLayoutManager(CollectActivity.this, 1));
        adapter = new MecollectAdapter(list, CollectActivity.this);
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                godeitls(list.get(position).getDuanzi_id() + "");
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getdata(imei, page + "");
            }
        }, mRecyclerView);
    }

    private void godeitls(String ids) {
        Intent intent = new Intent(CollectActivity.this, DuanziDetilsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", ids);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        imei = PreferenceUtil.getString("imei", "");
        if (TextUtils.isEmpty(imei)) {
            T.showShort(CollectActivity.this, "未获取到imei权限，请从设置打开授予权限！");
            return;
        }
        if (mPresenter != null) {
            showLoadingDialog();
            mPresenter.getdata(imei, page + "");
        }
        collectBack.setOnClickListener(v -> finish());
    }

    @Override
    public void loaddata(MeCollectBean meCollectBean) {
        hideLoadingDialog();
        if (meCollectBean.getStatus() == 1) {
            list.clear();
            list = meCollectBean.getData();
            adapter.setNewData(list);
            mPtrFrameLayout.refreshComplete();
            showSuccess();
            page++;
            if (meCollectBean.getData().size()<3){
                adapter.loadMoreEnd();
            }
        } else {
            T.showShort(CollectActivity.this, meCollectBean.getMsg());
        }
    }

    @Override
    public void laodmore(MeCollectBean meCollectBean) {
        if (meCollectBean.getStatus() == 1 && meCollectBean.getData().size() != 0) {
            page++;
            adapter.addData(meCollectBean.getData());
            adapter.loadMoreComplete();
        } else if (meCollectBean.getData() == null || meCollectBean.getData().size() == 0) {
            adapter.loadMoreEnd();
            T.showShort(CollectActivity.this, "没有更多数据了！");
            return;
        } else {
            T.showShort(CollectActivity.this, meCollectBean.getMsg());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
